package com.terenac;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
//import biz.source_code.base64Coder.*;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.widget.TextView;
import android.widget.Toast;

public class HttpStream {
	
	// Returns JSON String
	public String PostRequest(String uri, List<NameValuePair> postData) throws Exception{
		BufferedReader in = null;
		String data = null;
		
			HttpClient client = new DefaultHttpClient();
			URI website = new URI(uri);
			HttpPost request = new HttpPost();
			request.setURI(website);
			
			//request.setHeader("Content-type", "multipart/form-data");
			UrlEncodedFormEntity urlForPost = new UrlEncodedFormEntity(postData);
			request.setEntity(urlForPost);
			
			HttpResponse response = null;
			try {
				response = client.execute(request);
			} catch (Exception e) {
				//no net exception
				throw new Exception("No internet connection");
			}
			data = responseToString(response);
			return data;

	}
	

	public String postMultipartPicture (String uri, List<NameValuePair> userData, Bitmap picture) throws Exception{
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		HttpPost request = new HttpPost();
		URI website = null;
		try {
			website = new URI(uri);
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			
		}
		request.setURI(website);
		HttpResponse response = null;
		
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		picture.compress(Bitmap.CompressFormat.JPEG, 10, stream);
		byte[] bitmapdata = stream.toByteArray();
		ByteArrayBody body = new ByteArrayBody(bitmapdata, "pic.jpg");

		try{
			MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			
			for(int index=0; index < userData.size(); index++) {	                
	                entity.addPart(userData.get(index).getName(), new StringBody(userData.get(index).getValue()));	       
			}
			entity.addPart("picture", body );
			
			request.setEntity(entity);
			response = httpClient.execute(request, localContext);			
		} catch (IOException e){
			throw new Exception("Problem with upload.");
		}
		String data = null;
		data = responseToString(response);
		return data;
	}

	public String responseToString(HttpResponse resp){
		BufferedReader in = null;
		String data = null;
		
		try {
			in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String newLine = System.getProperty("line.separator");
			while ((line = in.readLine()) != null){
				sb.append(line + newLine);
			}
			in.close();
			data = sb.toString();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;

	}
}
